package com.yufu.navigation.bar;

import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class NavigationBar {

    private int currentNavigationBarColor;
    private int currentSatusBarColor;
    private final Window window;

    public NavigationBar(AppCompatActivity activity) {
        this.window = activity.getWindow();
        this.currentNavigationBarColor = activity.getWindow().getNavigationBarColor();
    }

    public void hide() {
        View decorView = window.getDecorView();
        int uiOptions = decorView.getSystemUiVisibility();
        uiOptions = uiOptions | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        uiOptions = uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE;
        decorView.setSystemUiVisibility(uiOptions);
    }

    public void show() {
        View decorView = window.getDecorView();
        int uiOptions = decorView.getSystemUiVisibility();
        uiOptions = uiOptions & ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
        uiOptions = uiOptions & ~View.SYSTEM_UI_FLAG_IMMERSIVE;
        decorView.setSystemUiVisibility(uiOptions);
    }

    public void setOverlaysWebView(Boolean overlays) {
        View decorView = window.getDecorView();
        int uiOptions = decorView.getSystemUiVisibility();
        if (overlays) {
            uiOptions = uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            uiOptions = uiOptions | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(uiOptions);
            currentNavigationBarColor = window.getNavigationBarColor();
            currentSatusBarColor = window.getStatusBarColor();
            window.setNavigationBarColor(Color.TRANSPARENT);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            uiOptions = uiOptions & ~View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            uiOptions = uiOptions & ~View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(uiOptions);
            window.setNavigationBarColor(currentNavigationBarColor);
            window.setStatusBarColor(currentSatusBarColor);
        }
    }

    public NavigationBarInfo getInfo() {
        View decorView = window.getDecorView();
        NavigationBarInfo info = new NavigationBarInfo();
        info.setOverlays(
                (decorView.getSystemUiVisibility()
                        & View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION) == View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        info.setVisible(
                (decorView.getSystemUiVisibility()
                        & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        info.setColor(String.format("#%06X", (0xFFFFFF & window.getNavigationBarColor())));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            info.setDividerColor(String.format("#%06X", (0xFFFFFF & window.getNavigationBarDividerColor())));
        }
        return info;
    }

}
